<?php 


/**
* 信息处理基础类
*/
class BaseMsgDisposer 
{


	//获取文本类型返回信息
	public function getTextResult($fromUsername, $toUsername,$replyContent){

		$time = time();
		$msgType = "text";
		$textTpl = "<xml>
						<ToUserName><![CDATA[%s]]></ToUserName>
						<FromUserName><![CDATA[%s]]></FromUserName>
						<CreateTime>%s</CreateTime>
						<MsgType><![CDATA[%s]]></MsgType>
						<Content><![CDATA[%s]]></Content>
					</xml>";   

		$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $replyContent);

		return $resultStr;
	}


	//获取图片类型返回信息
	public function getImageResult($fromUsername, $toUsername,$media_id){

		$time = time();
		$msgType = "image";
		$textTpl = "<xml>
						<ToUserName><![CDATA[%s]]></ToUserName>
						<FromUserName><![CDATA[%s]]></FromUserName>
						<CreateTime>%s</CreateTime>
						<MsgType><![CDATA[%s]]></MsgType>
						<Image>
							<MediaId><![CDATA[%s]]></MediaId>
						</Image>
					</xml>";   

		$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $media_id);

		return $resultStr;
	}


	// 获取图文类型返回信息
	public function getNewsResult($fromUsername, $toUsername,$items=array()){

		if(empty($items)) return "";

		$count=count($items);

		$time = time();
		$msgType = "news";
		$textTpl = "<xml>
						<ToUserName><![CDATA[%s]]></ToUserName>
						<FromUserName><![CDATA[%s]]></FromUserName>
						<CreateTime>%s</CreateTime>
						<MsgType><![CDATA[%s]]></MsgType>
						<ArticleCount>$count</ArticleCount>
						<Articles>";			

		foreach ($items as $key => $value) {			
			
			$title=$value["title"];
			$description=$value["description"];
			$picurl=$value["picurl"];
			$url=$value["url"];

			$item_str="<item>
							<Title><![CDATA[$title]]></Title> 
							<Description><![CDATA[$description]]></Description>
							<PicUrl><![CDATA[$picurl]]></PicUrl>
							<Url><![CDATA[$url]]></Url>
						</item>	";

			$textTpl=$textTpl.$item_str;
		}

		$textTpl=$textTpl."</Articles></xml>";

		$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType);

		return $resultStr;

	}
}

